package com.warom.sdg.websocket.handler;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.gson.JsonObject;
import com.warom.sdg.R;
import com.warom.sdg.util.LogUtils;

/**
 * 通知消息处理器
 * 负责处理WebSocket接收到的通知类型消息
 */
public class NotificationHandler {
    private static final String TAG = "NotificationHandler";
    
    private final Context context;
    
    /**
     * 构造函数
     * @param context 上下文
     */
    public NotificationHandler(Context context) {
        this.context = context;
    }
    
    /**
     * 处理通知消息
     * @param title 通知标题
     * @param message 通知内容
     * @param data 通知数据
     */
    public void handleNotification(String title, String message, JsonObject data) {
        Log.d(TAG, "处理通知: " + title + " - " + message);
        
        // 记录日志
        LogUtils.logOperation("WebSocket", "通知", "收到通知: " + title + " - " + message);
        
        // 显示系统通知
        showNotificationMessage(title, message);
    }
    
    /**
     * 显示系统通知
     * @param title 通知标题
     * @param message 通知内容
     */
    private void showNotificationMessage(String title, String message) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "message_channel",
                    "消息通知",
                    NotificationManager.IMPORTANCE_DEFAULT);
            
            NotificationManager notificationManager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
                
                Notification notification = new NotificationCompat.Builder(context, "message_channel")
                        .setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setAutoCancel(true)
                        .build();
                
                notificationManager.notify((int) System.currentTimeMillis(), notification);
            }
        } else {
            // 对于Android 8.0以下版本，使用旧的通知API
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
            
            NotificationManager notificationManager = 
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            
            if (notificationManager != null) {
                notificationManager.notify((int) System.currentTimeMillis(), builder.build());
            }
        }
    }
} 